package cn.edu.fzu.homemaking.chat.mq;

import cn.edu.fzu.homemaking.chat.config.ChatConfig;
import cn.edu.fzu.homemaking.chat.domain.dto.MessageDto;
import cn.edu.fzu.homemaking.chat.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ChatMessageMQ {

    private static final Logger log = LoggerFactory.getLogger(ChatMessageMQ.class);

    @Resource
    private MessageService messageService;

    //监听chat message队列
    @RabbitListener(queues = { ChatConfig.QUEUE_CHAT_MESSAGE })
    public void receive(MessageDto dto) {
        try {
            messageService.add(dto.getFromId(), dto.getToId(), dto.getText(), dto.getCreateTime(), dto.isRead(),
                    dto.getOrigin());
        } catch (Exception e) {
            log.error("消息保存失败:{}",dto,e);
        }
    }

}
